package aod.android.com.bernay.mine.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import aod.android.com.bernay.R;
import aod.android.com.bernay.mine.module.DynamicBean;
import aod.android.com.bernay.mine.module.ProblemBean;

/**
 * @author crated by zhixianuanyang on 2019/4/19 12:11
 * @Email jamesKing0812@gmail.com
 */
public class ProblemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    //定义三种常量  表示三种条目类型
    public static final int VIEW_TYPE_ITEM = 0;
    public static final int VIEW_TYPE_EMPTY = 1;
    private final Context mContext;
    private TextView mContents;
    private final List<ProblemBean.DataBean> problemList;

    public ProblemAdapter(Context context, List<ProblemBean.DataBean> problemList) {
        this.mContext = context;
        this.problemList = problemList;
    }

    @Override
    public int getItemCount() {
        if (problemList.size() == 0) {
            return 1;
        }
        //如果不为0，按正常的流程跑
        return problemList.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (problemList.size() <= 0){
            return VIEW_TYPE_EMPTY;
        }else{
            return VIEW_TYPE_ITEM;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        LayoutInflater mInflater = LayoutInflater.from(mContext);
        RecyclerView.ViewHolder holder = null;
        if (viewType == VIEW_TYPE_EMPTY){
            View view = mInflater.inflate(R.layout.empty_recycle_view, parent, false);
            holder = new RecyclerView.ViewHolder(view) {};
        }else if(viewType == VIEW_TYPE_ITEM){
            View view = mInflater.inflate(R.layout.problem_list_item, parent, false);
            holder = new ProblemViewHolder(view,mContext);
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof ProblemViewHolder ){
            ProblemBean.DataBean dynamicBean = problemList.get(position);
            ((ProblemViewHolder) holder).bindData(dynamicBean);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener !=null){
                    onItemClickListener.onItemClick(position);
                }
            }
        });
    }

    //viewholder
    class ProblemViewHolder extends RecyclerView.ViewHolder{

        public ProblemViewHolder(View itemView,Context mContext) {
            super(itemView);
            mContents = itemView.findViewById(R.id.tv_contents);
        }

        public void bindData(ProblemBean.DataBean dynamicBean) {
            mContents.setText(dynamicBean.getContent());
        }
    }

    private OnItemClickListener onItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

}
